package com.ruoyi.radar.cal;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.radar.domain.RadarSilo;
import com.ruoyi.radar.domain.RadarSiloReal;
import com.ruoyi.radar.service.RadarSiloConfService;
import com.ruoyi.radar.service.RadarSiloRealService;
import com.ruoyi.radar.service.RadarSiloResultService;
import com.ruoyi.radar.service.RadarSiloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ReadPlyFile  implements CommandLineRunner {

    @Autowired
    RadarSiloConfService radarSiloConfService;
    @Autowired
    RadarSiloService radarSiloService;
    @Autowired
    RadarSiloResultService radarSiloResultService;
    @Autowired
    RadarSiloRealService radarSiloRealService;

    @Override
    public void run(String... args) throws Exception {
//        this.connectPushTcp();
//        this.updateIsOnline();
//        readPly();
    }

    Float minx = null;
    Float maxx = null;
    Float miny = null;
    Float maxy = null;
    Float minz = null;
    Float maxz = null;

    public void readPly(){
        try {
            String rootDir = System.getProperty("user.dir");
            String filename = "out_grid.ply";
            String filePath = rootDir + File.separator + filename;
            PlyHeader header = new PlyHeader();
            header.parseHeader(filePath);

            PlyReader reader = new PlyReader(header);
            List<PlyReader.Vertex> vertices = reader.readVertices(filePath);

            ArrayList<double[]> newList = processVertices(vertices);


            saveModel(newList,filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public ArrayList<double[]> processVertices(List<PlyReader.Vertex> vertices) {
        // 示例：打印所有顶点坐标
        ArrayList<double[]> newList = new ArrayList<>();

        for (PlyReader.Vertex vertex : vertices) {
//            System.out.println("Vertex: (" + vertex.x + ", " + vertex.y + ", " + vertex.z + ")");
            if(minx == null || vertex.x < minx) minx = vertex.x;
            if(maxx == null || vertex.x > maxx) maxx = vertex.x;
            if(miny == null || vertex.y < miny) miny = vertex.y;
            if(maxy == null || vertex.y > maxy) maxy = vertex.y;
            if(minz == null || vertex.z < minz) minz = vertex.z;
            if(maxz == null || vertex.z > maxz) maxz = vertex.z;
            newList.add(new double[]{vertex.x,vertex.y,vertex.z});
        }


        return newList;
    }

    public void saveModel(List<double[]> data,String fileName) {
        RadarSilo radarSilo = new RadarSilo();
//        radarSilo.setSiloStockId(8);
        String[] split = fileName.split(".");
        radarSilo.setSiloStockName("out_grid");
        radarSilo.setSiloMaterial("煤");
        radarSilo.setSiloDensity("1");
        radarSilo.setSiloHeight("45");
//        radarSilo.setCreateTime(commonUtil.currentTime());
//        radarSilo.setUpdateTime(commonUtil.currentTime());
        boolean b = radarSiloService.updateById(radarSilo) || radarSiloService.save(radarSilo);

        RadarSiloReal radarSiloReal = new RadarSiloReal();
        radarSiloReal.setStockId("8");
//        radarSiloReal.setTimeLong(obj.get("TimeLong").toString());
//        radarSiloReal.setLinerLev(obj.get("LinerLev").toString());
//        radarSiloReal.setLinerVol(obj.get("LinerVol").toString());
//        radarSiloReal.setLinerWeigh(obj.get("LinerWeigh").toString());
//        radarSiloReal.setPerLevel(obj.get("PerLevel").toString());
        radarSiloReal.setLinerMax(maxz.toString());
        radarSiloReal.setLinerMin(minz.toString());
//        radarSiloReal.setWarning(obj.get("Warning").toString());
//        radarSiloReal.setWarningLevel(obj.get("WarningLevel").toString());


        radarSiloReal.setSiloSurface(JSONArray.toJSONString(data));
        boolean b1 = radarSiloRealService.updateById(radarSiloReal) || radarSiloRealService.save(radarSiloReal);
    }

}
